package com.stockz.equity.charting.painter.indicator;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import org.springframework.stereotype.Component;

import com.stockz.equity.charting.display.Displayable;
import com.stockz.equity.charting.util.ContextUtil;
import com.stockz.equity.charting.view.StockChartPanelState;
import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.pattern.Painter;

@Component
public class IndicatorControlPainter implements Painter<Displayable> {

	@Override
	public void paint(Graphics2D g, Displayable indicatorDisplay, Rectangle rectangle) {

		int paddingX = 6;
		int paddingY = 6;
		int spacingY = 5;
		int arc = 8;
		
		StockChartPanelState state = ((StockChartView)ContextUtil.getApplicationContext().getBean(StockChartView.class))
		.getSelectedStockChart().getStockChartPanel().getStockChartPanelState();
				
		int order = 0;
		int priceDisplayableCount = state.getPriceRectangle().getDisplayableSet().size();
		if(state.getPriceRectangle().equals(indicatorDisplay.getRectangle())){
			order = indicatorDisplay.getOrder();
		}else if(state.getVolumeRectangle().equals(indicatorDisplay.getRectangle())){
			order = priceDisplayableCount + indicatorDisplay.getOrder();
		}
		
		g.setClip(rectangle);
		String text = indicatorDisplay.getDescription();
		double textHeight = g.getFont().getStringBounds(text, g.getFontRenderContext()).getHeight();
		double controlRectangleHeight = textHeight + paddingY;
		
		String remove = "Remove";
		double removeTextWidth = g.getFont().getStringBounds(remove, g.getFontRenderContext()).getWidth(); 
		double removeTextHeight = g.getFont().getStringBounds(remove, g.getFontRenderContext()).getHeight(); 
		double removeRectangleWidth = removeTextWidth + paddingX;
		int removeRectangleX = rectangle.x + 5;
		int removeRectangleY = (int)(rectangle.y + order*(controlRectangleHeight + spacingY));
		g.setColor(Color.red);
		g.fillRoundRect(removeRectangleX, 
				removeRectangleY, 
				(int)removeRectangleWidth, 
				(int)controlRectangleHeight, arc, arc);
		indicatorDisplay.setRemoveRectangle(new Rectangle(
				removeRectangleX,
				removeRectangleY,
				(int)removeRectangleWidth,
				(int)controlRectangleHeight
		));
		int removeTextX = (int)(removeRectangleX + (removeRectangleWidth - removeTextWidth)/2);
		int removeTextY = (int)(removeRectangleY + removeTextHeight + (controlRectangleHeight - removeTextHeight)/2);
		g.setColor(Color.black);
		g.drawString(remove, removeTextX, removeTextY);
		
		
		if(indicatorDisplay.getConfiguration()==null){
			g.setColor(indicatorDisplay.getColor());
			int textX = (int)(removeRectangleX + removeRectangleWidth + 10);
			int textY = removeTextY;
			g.drawString(text, textX, textY);
		}else{
			String edit = "Edit";
			double editTextWidth = g.getFont().getStringBounds(edit, g.getFontRenderContext()).getWidth(); 
			double editTextHeight = g.getFont().getStringBounds(edit, g.getFontRenderContext()).getHeight(); 
			double editRectangleWidth = editTextWidth + paddingX;
			int editRectangleX = (int)(removeRectangleX + removeRectangleWidth + 10);
			int editRectangleY = removeRectangleY;
			g.setColor(indicatorDisplay.getColor());
			g.fillRoundRect(editRectangleX, 
					editRectangleY, 
					(int)editRectangleWidth, 
					(int)controlRectangleHeight, arc, arc);
			indicatorDisplay.setEditRectangle(new Rectangle(
					editRectangleX,
					editRectangleY,
					(int)editRectangleWidth,
					(int)controlRectangleHeight
			));
			
			int editTextX = (int)(editRectangleX + (editRectangleWidth - editTextWidth)/2);
			int editTextY = (int)(editRectangleY + editTextHeight + (controlRectangleHeight - editTextHeight)/2);
			g.setColor(Color.black);
			g.drawString(edit, editTextX, editTextY);
			
			
			
			g.setColor(indicatorDisplay.getColor());
			int textX = (int)(editRectangleX + editRectangleWidth + 10);
			int textY = editTextY;
			g.drawString(text, textX, textY);
		}
	}
}
